package jgo.plugins.middleware;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jgo.Context;
import jgo.Handler;
import jgo.JGo;
import jgo.core.MiddlewarePlugin;

public class ErrorHandlingMiddleware extends MiddlewarePlugin {
	private static final Logger logger = LoggerFactory.getLogger(ErrorHandlingMiddleware.class);

	@Override
	public void init(JGo gin) {
		// 初始化逻辑（如果需要）
	}


	@Override
	public void handle(Context ctx, Handler next) {
		try {
			next.handle(ctx);
		} catch (Exception e) {
			logger.error("An error occurred while processing the request", e);
			ctx.status(500).json("Internal Server Error");
		}
	}
}